KM2 can be used to generated weird stuff

1. WEIRD STUFF WITH KM2



Other presentations are also available on the web :
KM2 allows fancy (but very efficient) programming.

1.1. Some quick examples

using greek characters and usual math notation :
This is a python program

1.2. Programming with KM2

1.2.1. Programming with tables

Suppose we want to write some similar functions that are very similar.
We define a table as a variable :

SOURCE IN KM2

First define the table of functions as a tree variable :

Generated by KM2 :

The table can now appear in the text :
name stop stop_value recursion
fact n < 1 1 n * fact (n-1)
fib n < 2 1 fib (n-1) + fib (n-2)
sum_squares n < 1 0 n*n + sum_squares (n-1)

Now we can define all the functions with just a macro
SOURCE IN KM2


and the extraction macro for python

Generated by KM2 :

def fact (n) :
    if n < 1 :
        return 1
    else :
        return n * fact (n-1)
def fib (n) :
    if n < 2 :
        return 1
    else :
        return fib (n-1) + fib (n-2)
def sum_squares (n) :
    if n < 1 :
        return 0
    else :
        return n*n + sum_squares (n-1)

And also some js code :
SOURCE IN KM2


and the extraction macro for javascript

Generated by KM2 :

function fact (n) {
    if (n < 1) {
        return 1;
        }
    else {
        return n * fact (n-1);
    }
}
function fib (n) {
    if (n < 2) {
        return 1;
        }
    else {
        return fib (n-1) + fib (n-2);
    }
}
function sum_squares (n) {
    if (n < 1) {
        return 0;
        }
    else {
        return n*n + sum_squares (n-1);
    }
}

1.2.2. The 'WHERE' syntax

SOURCE IN KM2

A simple example

Generated by KM2 :


1.3. Writing documents with KM2

Suppose we want to write some more documentation about the functions defined above
We redefine a table as a variable :
Now we generate the code with the documentation

SOURCE IN KM2

First define the table of functions as a tree variable :

and the extraction macro for python

Generated by KM2 :





SOURCE IN KM2

First we improve slightly the documentation with more information, hyperlinks and graphics:

Now we can also generate 'on the fly', more documentation for the code :

The output is not in the same order as in the table or in the declaration
Also we can see that programm sources can not embed documenttaion with graphics.

Generated by KM2 :

#INFORMATIONS
#---  fact (n) ---
#tested ok
#---  fib (n) ---
#Here, fib is computed in fib (n) time
#    can be computed in log (n) time
#---  sum_squares (n) ---
#tested ok
#NOW THE CODE

But we can do better (cf below) !

Now, we can generate text for a better documentation :
SOURCE IN KM2




Generated by KM2 :

List of functions :

-- fact(n)
-- fib(n)
-- sum_squares(n)
--- function fact (n) ---
Documentation :
the factorial function

Notes :
        tested ok

--- function fib (n) ---
Documentation :
the fibonacci function

Notes :
        Here, fib is computed in fib (n) time
                can be computed in log (n) time

--- function sum_squares (n) ---
Documentation :
The sum of integer squares up to n there is also a polynomial formula

Notes :
        tested ok

1.4. Writing CSS with tables

Writing CSS styles is quite painful and difficult.
KM2 allows to use tables which are easier to define, read and maintain.

STEP 1 : define the selectors and their values :
Example of css styles for headers
The values of variables can be defined as variabe in the file or by defining palettes :

Of course, we define several tables similarly for navbar, articles, etc

Now we describe the macros and generate the file :
palette using css variables at the beginning of the file

STEP 2 : write macros to generate the css
Macros for the generation of css files

Step 3 : generate the css file
We get :

and :


Done.

Remember that you write the macros once, and only change tables !





...